package com.easyUploader.util;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @author 大忽悠
 * @create 2022/11/16 10:42
 */
public class RedisUtil {
   private static StringRedisTemplate redisTemplate= SpringUtil.getBean(StringRedisTemplate.class);

   /**
    * @param key
    * @param value
    * @param ex 过期时间
    */
   public static void setKeyEx(String key, String value, Long ex, TimeUnit timeUnit){
      redisTemplate.opsForValue().set(key,value,ex,timeUnit);
   }

   /**
    * 过期时间单位为分钟
    */
   public static void setKeyEx(String key, String value, Long ex){
      redisTemplate.opsForValue().set(key,value,ex,TimeUnit.MINUTES);
   }

   /**
     key存在则返回,不存在,返回null
    */
   public static String getKey(String key){
      return redisTemplate.opsForValue().get(key);
   }

    public static void delKey(String codeKey) {
          redisTemplate.delete(codeKey);
   }
}
